package com.useeinfo.oa.modules.base.biz;

import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.oa.modules.base.dao.AppVersionDao;
import com.useeinfo.oa.modules.base.entity.AppVersion;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AppVersionBiz extends CrudBiz<AppVersionDao, AppVersion> {

    public Boolean exist(AppVersion appVersion) {
        if (StringUtils.isBlank(appVersion.getDeviceType()) || StringUtils.isBlank(appVersion.getSurroundings())) {
            return true;
        }
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("deviceType", appVersion.getDeviceType());
        queryParam.getSqlMap().put("surroundings", appVersion.getSurroundings());
        List<AppVersion> appVersions = findList(queryParam);

        if (appVersion.getId() == null) {
            return CollectionUtils.isNotEmpty(appVersions);
        } else {
            return !((appVersions.size() == 1 && appVersions.get(0).getId().equals(appVersion.getId())) || appVersions.size() == 0);
        }
    }
}
